package com.foreveross.proxyip.core.http.ipdoload.db;

import java.lang.reflect.InvocationTargetException;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.dayatang.querychannel.service.QueryChannelService;
import com.foreveross.proxyip.core.http.ipdoload.AbsProxyIpOriginal;
import com.foreveross.proxyip.entity.ExtractLogEntity;
import com.foreveross.proxyip.entity.IpStoreEntity;
import com.foreveross.proxyip.entity.IpWebsiteEntity;
import com.foreveross.proxyip.entity.ProxyIpBean;
import com.foreveross.proxyip.infra.base.ddd.repository.IBaseRepository;

/**
 * 本地历史检索
 * @author lyq
 *
 */
@Service("historyIndex")
public class HistoryIndex extends AbsProxyIpOriginal  {
	
	private static final long serialVersionUID = 1L;
	@Resource(name="queryChannel")
	QueryChannelService queryChannelService ;
	/**
	 * 
	 */
	
	@Autowired
	public HistoryIndex(@Qualifier("entityHibernateRepository")IBaseRepository  baseRepository){
		super.baseRepository = baseRepository;
	}

	/**
	 * 从数据库获取i条源ip 修改数据库存在于公共池state为1
	 * @throws InvocationTargetException 
	 * @throws IllegalAccessException 
	 */
	@Transactional
	public void extract(Map<String,ProxyIpBean> proxyIpMap,IpWebsiteEntity website,Integer num)  throws Exception {
		 super.extract(proxyIpMap,website,num);
	}


	
	@SuppressWarnings("static-access")
	public void loadProxyIpMap(Map<String,ProxyIpBean> proxyIpMap,ExtractLogEntity extractLogEntity,Integer num) throws Exception {
		Date date=new Date();//取时间
		 Calendar calendar = new GregorianCalendar();
		 calendar.setTime(date);
		 calendar.add(calendar.DATE,-1);
		 date=calendar.getTime(); //日期往前推一天的结果 
		 
		Object []params = null;
		List<IpStoreEntity> usedList = queryChannelService.queryResult("from IpStoreEntity t order by t.lastTime", params, 1L, num);
//		List <ProxyIpBean> resultList = new ArrayList<ProxyIpBean>();
		extractLogEntity.setExtractNum(usedList==null?0:usedList.size());
		for(IpStoreEntity ipStoreEntity : usedList ){
			ipStoreEntity.setState(1);
			ipStoreEntity.setLastTime(new Date());
			ProxyIpBean bean = new ProxyIpBean();
			BeanUtils.copyProperties(bean, ipStoreEntity);
			proxyIpMap.put(bean.getCompletionIP(), bean);
//			resultList.add(bean);
		}
		
//		return resultList;
	}

	@Override
	public void loadProxyIpMap(Map<String, ProxyIpBean> proxyIpMap,
			IpWebsiteEntity wegsite,ExtractLogEntity extractLogEntity, Integer num) throws Exception {
		this.loadProxyIpMap(proxyIpMap,extractLogEntity, num);
	}

	@Override
	public Object[] httpGetIPS(IpWebsiteEntity website, Integer num)
			throws Exception {
		// TODO Auto-generated method stub
		return null;
	}	

}
